package com.eboss2.sbs.pushali;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Build;
import android.support.v4.app.NotificationCompat;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.eboss2.sbs.R;
import com.eboss2.sbs.Tts.BdTts;
import com.eboss2.sbs.activity.ActivityOrderDetail;
import com.eboss2.sbs.activity.ExamineManagerActivity;
import com.eboss2.sbs.activity.PointTransDetailActivity;
import com.eboss2.sbs.activity.ReceiptInfoActivity;
import com.eboss2.sbs.activity.ServiceGoodsOrderListManagerActivity;
import com.eboss2.sbs.config.Config;
import com.tool.utils.utils.StringUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

/**
 * Created by Administrator on 2018/1/23 0023.
 */

public class MyMessageReceiver extends MessageReceiver {
    // 消息接收部分的LOG_TAG
    public static final String REC_TAG = "receiver";
    private  PendingIntent pendingIntent;
//    private int mNotificationNum = 1;

    /**
     * isplay:
     0=不播放语音，1=播放语音

     type:
     1=订单,2=钱包，3=普通通知

     tvalue：
     type=1，该值为订单号，对应t_trade_offline中的order_id
     type=2,无值，跳转到钱包交易列表
     type=3,无值，跳转到通知列表

     * @param context
     * @param title
     * @param summary
     * @param extraMap
     */

    @Override
    public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
        // TODO 处理推送通知
        Log.e("MyMessageReceiver", "Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap);

        String playStr = summary;
        String isplay = extraMap.get("isplay");

        setNotice(context, title, summary, extraMap);

        if (StringUtils.isEquals("1", isplay)) {
            BdTts.getInstance(context).speak(playStr);
        }

    }

    private void setNotice(Context context, String title, String content, Map<String, String> extraMap){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {

           String type = extraMap.get("type");
           String tvalue = extraMap.get("tvalue");
//           PendingIntent pendingIntent = null;
            Config.mNotificationNum++;

            switch (type){
                case "1":
                    if (!StringUtils.isBlank(tvalue)) {//.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        Intent intent = new Intent(context.getApplicationContext(), ActivityOrderDetail.class).putExtra("orderId", Long.valueOf(tvalue));
                        intent.addCategory(Intent.CATEGORY_LAUNCHER).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                                context.getApplicationContext().startActivity(new Intent(context.getApplicationContext(), ActivityOrderDetail.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).putExtra("orderId", Long.valueOf(tvalue)));
                        pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), Config.mNotificationNum, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                    }
                    break;
                case "4":
                    if (!StringUtils.isBlank(tvalue)) {//.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                        String [] temp = tvalue.split("#");
//                        Bundle bundle = new Bundle();
//                        bundle.putLong("tradeId", Long.valueOf(temp[0]));
//                        bundle.putLong("memberId", Long.valueOf(temp[1]));
                        Intent intent = new Intent(context.getApplicationContext(), PointTransDetailActivity.class).putExtra("tradeId", Long.valueOf(temp[0])).putExtra("memberId", Long.valueOf(temp[1]));
                        intent.addCategory(Intent.CATEGORY_LAUNCHER).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//                                context.getApplicationContext().startActivity(new Intent(context.getApplicationContext(), ActivityOrderDetail.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).putExtra("orderId", Long.valueOf(tvalue)));
                        pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), Config.mNotificationNum, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                    }
                    break;
                case "2": {
                    Intent intent = new Intent(context.getApplicationContext(), ReceiptInfoActivity.class);
                    intent.addCategory(Intent.CATEGORY_LAUNCHER).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), Config.mNotificationNum, intent, PendingIntent.FLAG_UPDATE_CURRENT);

//                            context.getApplicationContext().startActivity(new Intent(context.getApplicationContext(), ReceiptInfoActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                }
                    break;
                case "3": {
                    Intent intent = new Intent(context.getApplicationContext(), ExamineManagerActivity.class);
                    intent.addCategory(Intent.CATEGORY_LAUNCHER).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), Config.mNotificationNum, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//                            context.getApplicationContext().startActivity(new Intent(context.getApplicationContext(), ExamineManagerActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                }
                    break;
                case "5": {
                    Intent intent = new Intent(context.getApplicationContext(), ServiceGoodsOrderListManagerActivity.class);
                    intent.addCategory(Intent.CATEGORY_LAUNCHER).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    pendingIntent = PendingIntent.getActivity(context.getApplicationContext(), Config.mNotificationNum, intent, PendingIntent.FLAG_UPDATE_CURRENT);
//                            context.getApplicationContext().startActivity(new Intent(context.getApplicationContext(), ExamineManagerActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                }
                break;
            }



            NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            Notification notification = new NotificationCompat.Builder(context, "1")
                    .setContentTitle(title)
                    .setContentText(content)
                    .setWhen(System.currentTimeMillis())
                    .setSmallIcon(R.mipmap.icon)
                    .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.mipmap.icon))
                    .setAutoCancel(true)
                    .setContentIntent(pendingIntent)
                    .build();
            manager.notify(Config.mNotificationNum, notification);
        }
    }



    @Override
    public void onMessage(Context context, CPushMessage cPushMessage) {
        Log.e("MyMessageReceiver", "onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
    }
    @Override
    public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
        Log.e("MyMessageReceiver", "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);



        if (!TextUtils.isEmpty(extraMap)) {
            JSONObject customJson = null;
            try {
                customJson = new JSONObject(extraMap);

                String type = customJson.getString("type");
                String tvalue = customJson.getString("tvalue");

                switch (type){
                    case "1":
                        if (!StringUtils.isBlank(tvalue)) {
                            context.getApplicationContext().startActivity(new Intent(context.getApplicationContext(), ActivityOrderDetail.class).addCategory(Intent.CATEGORY_LAUNCHER).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).putExtra("orderId", Long.valueOf(tvalue)));
                        }
                        break;
                    case "4":
                        if (!StringUtils.isBlank(tvalue)) {//.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                            String [] temp = tvalue.split("#");
//                            Bundle bundle = new Bundle();
//                            bundle.putLong("tradeId", Long.valueOf(temp[0]));
//                            bundle.putLong("memberId", Long.valueOf(temp[1]));
                            context.getApplicationContext().startActivity(new Intent(context.getApplicationContext(), PointTransDetailActivity.class).addCategory(Intent.CATEGORY_LAUNCHER).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).putExtra("tradeId", Long.valueOf(temp[0])).putExtra("memberId", Long.valueOf(temp[1])));
                        }
                        break;
                    case "2":
                        context.getApplicationContext().startActivity(new Intent(context.getApplicationContext(), ReceiptInfoActivity.class).addCategory(Intent.CATEGORY_LAUNCHER).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                        break;
                    case "3":
                        context.getApplicationContext().startActivity(new Intent(context.getApplicationContext(), ExamineManagerActivity.class).addCategory(Intent.CATEGORY_LAUNCHER).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                        break;
                    case "5":
                        context.getApplicationContext().startActivity(new Intent(context.getApplicationContext(), ServiceGoodsOrderListManagerActivity.class).addCategory(Intent.CATEGORY_LAUNCHER).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
                        break;
                }

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }



    }
    @Override
    protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
        Log.e("MyMessageReceiver", "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
        // 自定义内容获取方式，mykey和myvalue对应通知推送时自定义内容中设置的键和值
//        String myvalue = null;
//        if (!TextUtils.isEmpty(extraMap)) {
//            JSONObject customJson = null;
//            try {
//                customJson = new JSONObject(extraMap);
//
//                if (!customJson.isNull("messageType")) {
//                    myvalue = customJson.getString("messageType");
//                }
//            } catch (JSONException e) {
//                // TODO Auto-generated catch block
//                e.printStackTrace();
//            }
//        }
//        updateContent(context, myvalue);
    }
    @Override
    protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
        Log.e("MyMessageReceiver", "onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
    }
    @Override
    protected void onNotificationRemoved(Context context, String messageId) {
        Log.e("MyMessageReceiver", "onNotificationRemoved");
    }


    private void updateContent(Context context, String messageType) {
        Log.d(TAG, "updateContent");
//        String logText = "" + Utils.logStringCache;
//
//        if (!logText.equals("")) {
//            logText += "\n";
//        }

//        TTSUtil.getInstance(context).play(content);

//        SimpleDateFormat sDateFormat = new SimpleDateFormat("HH-mm-ss");
//        logText += sDateFormat.format(new Date()) + ": ";
//        logText += content;
//
//        Utils.logStringCache = logText;
        if (StringUtils.isBlank(messageType)){
            return;
        }
//        //新订单
//        if (messageType.equals("2")) {
//            Intent intent = new Intent();
//            intent.setClass(context.getApplicationContext(), ActivityMessage.class);
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.getApplicationContext().startActivity(intent);
//        }else if (messageType.equals("1")){
//            Intent intent = new Intent();
//            intent.setClass(context.getApplicationContext(), ActivityHome.class);
//            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
//            context.getApplicationContext().startActivity(intent);
//        }
    }
}
